<?php

class Personal_Model_MUser
{
    private $_uid;
    private $_conn;
    private $_collection;
    private $_db;

    public function  __construct()
    {
        $this->_conn=new Mongo();
        $this->_db=$this->_conn->bookstore;
        $this->_collection=$this->_db->user_info;
    }
    public function getInfoBasic($uid)
    {
        $id=new MongoId($uid);
        return $this->_collection->findOne(array("_id"=>$id),array("_id"=>true,"user_name"=>true,"image"=>true));
    }
    public function getPersonalInfo($uid)
    {
        if(!$this->isExistedUser($uid))
            return array();

        $id=new MongoId($uid);
        return $this->_collection->findOne(array("_id"=>$id));
    }

    public function getPublicPersonalInfo($uid)
    {
        $id=new MongoId($uid);
        $collection=$this->_db->user_info_public;
        $res= $collection->findOne(array("_id"=>$id));
        if($res==null)
            return array();
        return $res;
    }
    // lấy danh sách bạn bè của $uid
    // thông tin bạn bè bao gồm: _id,username
    // phục vụ cho việc tặng quà khi mua sách
    public function getFriends($uid)
    {
        if(!$this->isExistedUser($uid))
            return array();
        $id=new MongoId($uid);
        $collection= $this->_db->user_friend;
        $friends=$collection->findOne(array("id_user"=>$id));

        if($friends ==null)
            return array();
        $arrFriend=array();
        foreach($friends["friend"] as $aFriend)
        {
            $user=$this->_collection->findOne(array("_id"=>$aFriend["id_user"]),
                    array("_id"=>true,"user_name"=>true));
            array_push($arrFriend, $user);
        }

        // insertion sort friend by name
        $function=new Default_Model_MFunction();
        $function->insertSort($arrFriend, "user_name");
        return $arrFriend;
    }

    public function setPersonalInfo($uid){
        /*
         * save and edit info of user
         */
    }
    public function getAccountInfo($uid){
        /*
         * get general info about account of user :
         * No of friend, No of sach duoc tang, No of sach duoc goi y, No of message
         */
    }
    public function isExistedUser($uid)
    {
        try
        {
            $id=new MongoId($uid);
            $docu=$this->_collection->findOne(array("_id"=>$id));
            if($docu!=null)
                return true;
            return false;
        }
        catch(Exception $e)
        {
            return false;
        }
    }
    public function register($arr_info=array())
    {
        if($arr_info["user_name"]!=null && $arr_info["user_name"]!='' && $arr_info["password"]!=null && $arr_info["password"]!=''
            && $arr_info["email"]!=null && $arr_info["email"]!='')
        {
            if($this->checkRegister($arr_info["user_name"],$arr_info["pin"],$arr_info["email"])==3)
            {
                $this->_collection->insert($arr_info);
                $temp=$this->_collection->findOne($arr_info);
                return $temp["_id"];
            }
        }
        return FALSE;
    }


    // kiểm tra thông tin đăng ký vên tên, cmtnd và email
    // return 0 nếu tên đăng nhập đã được sử dụng
    // return 1 nếu email đã được sử dụng
    // return 2 nếu cmtnd đã được sử dụng
    // return 3 nếu các thông tin trên có thể sử dụng
    public function checkRegister($user,$pin,$email)
    {
        $document=$this->_collection->findOne(array("user_name"=>$user));
        if($document!=null)
            return 0;
        if($pin!='' && $pin!=null)
        {
            $document=$this->_collection->findOne(array("pin"=>$pin));
            if($document!=null)
                return 2;
        }
        
        $document=$this->_collection->findOne(array("email"=>$email));
        if($document!=null)
            return 1;
        return 3;
    }
    private function checkLogin(){
        
    }
    public function login($username, $pass)
    {
        $number=$this->_collection->find(array("user_name"=>$username,"password"=>$pass))->count();
        if($number==0 || $number >1)
            return '';
        $document=$this->_collection->findOne(array("user_name"=>$username,"password"=>$pass));
        return $document["_id"];
    }
    public function storeViewHis($uid, $bid){
        
        $collection = $this->_db->book_view_history;
        $rating = -1;
        $newview = array("bid"=>$bid,"rating"=>$rating);
        $query = array("uid"=>$uid);
        $docU = $collection->findOne($query);
        if($docU != NULL){
            $existed = FALSE;
            for ($index = 0; $index < count($docU["lstbid"]); $index++) {
                    if($docU["lstbid"][$index]["bid"]==$bid){
                        $existed = TRUE;
                          break;
                    }
                }
                if(!$existed){
                    $select = array("uid"=>$uid);
                    $query = array('$push'=>array("lstbid"=>$newview));
                    $collection->update($select,$query);
                }
            }
            else {
                $doc = array("uid"=>$uid,"lstbid"=>array($newview));
                $collection->insert($doc);
            }
    }

    public function updateUserInfo($uid,$query=array())
    {
        if(!$this->isExistedUser($uid))
            return false;
        if(!is_array($query) || count($query)==0)
            return false;
        $id=new MongoId($uid);
        $this->_collection->update(array("_id"=>$id),array('$set'=>$query));
        return true;
    }

    public function sendMessage($uid,$uidReceive,$message)
    {
        if(!$this->isExistedUser($uid) || !$this->isExistedUser($uidReceive))
            return false;

        $messageCollection=$this->_db->user_message;
        // them tin nhan vao danh sach gui di cua nguoi gui
        
        // them tin nhan vao danh sach nhan duoc cua nguoi nhan
    }
}

